﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Threading.Tasks;
using System.Windows;
using MultiLanguage.Infrastructure;

namespace MultiLanguage
{
    public partial class App
    {
        private readonly List<SupportedCultureInfo> _cultures = new List<SupportedCultureInfo>();
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            string location = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            if (location != null)
            {// get the available cultures from the resource assembly....
                var culture = Assembly.LoadFile(Path.Combine(location, "CultureResources.dll"));
                var mnames = culture.GetManifestResourceNames();
                foreach (string name in mnames)
                {
                    var rm = new ResourceManager(name.Replace(".resources", String.Empty), culture);
                    string cultureName = rm.GetString("CultureName");
                    if (cultureName != null)
                    {
                        _cultures.Add(new SupportedCultureInfo(new CultureInfo(cultureName), rm));
                    }
                }
                // inject into the View Model
                ITemperatureConverter model = new TemperatureConverter();
                IViewModel vm = new TemperatureConverterViewModel(model, _cultures.OrderBy(q => q.DisplayName));
                // inject the DataContext and start the app
                new MainWindow { DataContext = vm }.Show();
            }
        }
        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);
            foreach (var sci in _cultures)
            {// per guidance, the resource managers MUST be released
                sci.Release();
            }
        }
    }
}
